﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System.ComponentModel;

namespace KD.RAISER.SS.OUTSTOCK.SK.TK
{
    [Description("销售出库单-单据加载后更新收款金额"), HotUpdate]
    public class OUTSTOCK_LOAD: AbstractDynamicFormPlugIn
    {
        public override void OnLoad(EventArgs e)
        {
            string FSOORDERNO, FSOENTRYID, FID, FSQL, FSEQ;
            string FREALRECAMOUNTFOR, FREALREFUNDAMOUNTFOR, FOUTQty, FRealQty;
            decimal F_SK_AMOUNT,FAmount;

            base.OnLoad(e);

            if (this.Model.GetValue("FBillNo") == null)
            {
                DynamicObjectCollection entryRows = this.View.Model.DataObject["SAL_OUTSTOCKENTRY"] as DynamicObjectCollection;

                if (entryRows != null && entryRows.Count > 0)
                {
                    for (int a = 0; a < entryRows.Count; a++)
                    {
                        if (entryRows[a]["SoorDerno"] != null && entryRows[a]["SOEntryId"] != null)
                        { 
                        FSOORDERNO = entryRows[a]["SoorDerno"].ToString();
                        FSOENTRYID = entryRows[a]["SOEntryId"].ToString();
                        FRealQty = entryRows[a]["RealQty"].ToString();

                        FSQL = "/*dialect*/ SELECT isnull(FID,0)  FROM T_SAL_ORDER WHERE FBILLNO ='" + FSOORDERNO + "'";
                        FID = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                        FSQL = "/*dialect*/ SELECT isnull(FSEQ,0)  FROM T_SAL_ORDERENTRY WHERE FID = " + FID + " AND FEntryID = " + FSOENTRYID + "";
                        FSEQ = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                        if (FSOORDERNO != "" && FSOENTRYID != "")
                        {
                            FSQL = "/*dialect*/ select isnull(sum(FREALRECAMOUNTFOR),0) from T_AR_RECEIVEBILLENTRY " +
                                   "where FSALEORDERNO ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                                   "and FID  in (select FID  from T_AR_RECEIVEBILL where  FDOCUMENTSTATUS ='C')";
                            FREALRECAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                            FSQL = "/*dialect*/ select isnull(sum(FREALREFUNDAMOUNTFOR),0) from T_AR_REFUNDBILLENTRY " +
                                    "where FSALEORDERNO ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                                    "and FID  in (select FID  from T_AR_REFUNDBILL where  FDOCUMENTSTATUS ='C')";
                            FREALREFUNDAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                            F_SK_AMOUNT = decimal.Parse(FREALRECAMOUNTFOR) - (decimal.Parse(FREALREFUNDAMOUNTFOR));

                            FSQL = "select /*dialect*/ ISNULL(SUM(FREALQTY),0) from T_SAL_OUTSTOCKENTRY WHERE  FENTRYID in (select FENTRYID  from T_SAL_OUTSTOCKENTRY_R where FSOORDERNO ='" + FSOORDERNO + "'  and  FSOENTRYID = " + FSOENTRYID + ")";
                            FOUTQty = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);


                            FAmount = F_SK_AMOUNT / (decimal.Parse(FOUTQty) + decimal.Parse(FRealQty)) * decimal.Parse(FRealQty);

                            this.Model.SetValue("F_SK_AMOUNT", FAmount, a);
                        }

                      }
                    }
                }
            }
        }
    }
}
